// pages/partySetting/partySetting.js
var util = require('../../utils/util.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    showMore: app.globalData.showMore,
    actType: "",
    dateFlag: false,
    dateTimeFlag: false,
    invitationPeriodFlag: false,
    invitationPeriodPanelFlag: false,
    peoplePanelFlag: false,
    peopelFlag: false,
    tranFlag: false,
    signFlag: false,
    activityFlag: false,
    regimentSize: 0,
    perUpperLimit: 0,
    invitationPeriod: -1,
    applyEndTime: '',
    applyEndTimeStr: '',
    currentEndDate: new Date().getTime(),
    currentEndTime: util.getTime(new Date()),
    minEndDate: new Date().getTime(),
    maxEndDate: new Date().getTime(),
    isCheck: 0,
    transportation: '',
    signInWay: '',
    activityProcess: '',
    activityProcessStr: '',
    tranList: [],
    signInList: [],
    columns: [{
        values: [],
        className: 'column1'
      },
      {
        values: ['1'].concat(Array.from({
          length: 99
        }, (v, k) => k + 2)),
        className: 'column2'
      },
      {
        values: ['至'],
        className: 'column3'
      },
      {
        values: ['不限'].concat(Array.from({
          length: 99
        }, (v, k) => k + 2)),
        className: 'column4'
      }, {
        values: [],
        className: 'column5'
      }
    ],
    invitationPeriodColumns: [{
        values: [],
        className: 'column1'
      },
      {
        values: [],
        className: 'column3'
      },
      {
        values: [].concat(Array.from({
          length: 48
        }, (v, k) => (k + 1) / 2)),
        className: 'column2'
      },
      {
        values: ['小时'],
        className: 'column4'
      }, {
        values: [],
        className: 'column5'
      }
    ],
  },
  //时间选择
  onPickerChange: function (e) {
    this.setData({
      applyEndTime: e.detail.dateString,
      applyEndTimeStr: e.detail.dateString,
      actEndTimeLong: new Date(e.detail.dateString).getTime()
    })
    console.log(this.data.applyEndTimeStr)
  },
  getTranList(options) {
    var that = this;
    app.req({
      url: 'wechat/dictionary/listBydictType',
      data: {
        dictType: 'ACT_TRANSPORT'
      },
      method: 'POST',
      success: function(res) {
        if (res.code == 200) {
          that.setData({
            tranList: res.data
          })
          if (options.transportation && options.transportation != '') {
            let transportation = ',' + options.transportation + ',',
              tranList = res.data;
            for (let item of tranList) {
              item.isChoose = transportation.indexOf(',' + item.dictValue + ',') >= 0;
            }
            that.setData({
              transportation: options.transportation,
              tranList: tranList,
              tranFlag: true
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getSignInList(options) {
    var that = this;
    app.req({
      url: 'wechat/dictionary/listBydictType',
      data: {
        dictType: 'PUNCH_IN'
      },
      method: 'POST',
      success: function(res) {
        if (res.code == 200) {
          that.setData({
            signInList: res.data
          })
          if (options.signInWay && options.signInWay != '') {
            let signIn = ',' + options.signInWay + ',',
              signInList = res.data;
            for (let item of signInList) {
              item.isChoose = signIn.indexOf(',' + item.dictValue + ',') >= 0;
            }
            that.setData({
              signInWay: options.signInWay,
              signInList: signInList,
              signFlag: true
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  tapRadio(e) {
    let name = e.currentTarget.dataset.name,
      ischoose = e.currentTarget.dataset.ischoose,
      value = e.currentTarget.dataset.value;
    this.toggle([name], !ischoose);
  },
  tapPeople(e) {
    this.setData({
      peoplePanelFlag: !this.data.peopelFlag,
      peopelFlag: !this.data.peopelFlag,
      regimentSize: !this.data.peopelFlag ? 0 : this.data.regimentSize,
      perUpperLimit: !this.data.peopelFlag ? 0 : this.data.perUpperLimit
    })
  },
  tapPeopleText(e) {
    this.setData({
      peoplePanelFlag: true
    })
  },
  peopleConfirm(e) {
    let regimentSize = e.detail.value[1] == '不限' ? -1 : e.detail.value[1],
      perUpperLimit = e.detail.value[3] == '不限' ? -1 : e.detail.value[3],
      min = regimentSize,
      max = (perUpperLimit == -1 ? 999 : perUpperLimit);
    if (min > max) {
      wx.showToast({
        title: '人员上限应大于人员下限',
        icon: 'none',
        duration: 2000
      })
    } else {
      this.setData({
        regimentSize: regimentSize,
        perUpperLimit: perUpperLimit
      })
      this.peopleCancel();
    }
  },
  peopleCancel(e) {
    this.setData({
      peoplePanelFlag: false
    })
  },
  tapInvitation(e) {
    this.setData({
      invitationPeriodPanelFlag: !this.data.invitationPeriodFlag,
      invitationPeriodFlag: !this.data.invitationPeriodFlag,
      invitationPeriod: this.data.invitationPeriodFlag ? -1 : this.data.invitationPeriod,
    })
  },
  tapInvitationText(e) {
    this.setData({
      invitationPeriodPanelFlag: true
    })
  },
  invitationConfirm(e) {
    this.setData({
      invitationPeriod: e.detail.value[2]
    })
    this.invitationCancel();
  },
  invitationCancel(e) {
    this.setData({
      invitationPeriodPanelFlag: false
    })
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  tapEndTime() {
    if (1 != this.data.status) {
      this.setData({
        dateFlag: true
      })
    }
  },
  onEndDateConfirm(event) {
    this.setData({
      applyEndTime: event.detail
    })
    this.setData({
      dateTimeFlag: true
    })
  },
  closeEndDate() {
    this.setData({
      dateFlag: false
    })
  },
  onEndTimeConfirm(event) {
    var time = util.toDate(util.getDate(this.data.applyEndTime) + ' ' + event.detail + ':00');
    this.setData({
      applyEndTime: time.getTime(),
      applyEndTimeStr: util.formatTime4(time.getTime())
    })
    this.setData({
      dateFlag: false,
      dateTimeFlag: false
    });
  },
  closeEndTime() {
    this.setData({
      dateTimeFlag: false
    })
  },
  tapCheck() {
    this.setData({
      isCheck: this.data.isCheck == 0 ? 1 : 0
    })
  },
  tapTran() {
    this.setData({
      tranFlag: !this.data.tranFlag
    })
  },
  tapSign() {
    this.setData({
      signFlag: !this.data.signFlag
    })
  },
  tapActivityProcess() {
    if (!this.data.activityFlag) {
      wx.navigateTo({
        url: '/pages/partySettingMemo/partySettingMemo?activityProcess=' + this.data.activityProcess
      })
    } else {
      this.setData({
        activityFlag: !this.data.activityFlag
      })
    }
  },
  save() {
    if (this.data.actType == 2 && this.invitationPeriodFlag && this.invitationPeriod == -1) {
      wx.showToast({
        title: '请选择邀请函有效期',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.actType == 1 && this.data.peopelFlag && (this.data.regimentSize == 0 || this.data.perUpperLimit == 0)) {
      wx.showToast({
        title: '请选择人员限制',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.tranFlag) {
      let transportation = [];
      for (let item of this.data.tranList) {
        if (item.isChoose) {
          transportation.push(item.dictValue);
        }
      }
      if (transportation.length == 0) {
        wx.showToast({
          title: '请选择交通工具',
          icon: 'none',
          duration: 2000
        })
        return;
      } else {
        this.setData({
          transportation: transportation.join(',')
        })
      }
    }
    if (this.data.signFlag) {
      let signIn = [];
      for (let item of this.data.signInList) {
        if (item.isChoose) {
          signIn.push(item.dictValue);
        }
      }
      if (signIn.length == 0) {
        wx.showToast({
          title: '请选择打卡签到方式',
          icon: 'none',
          duration: 2000
        })
        return;
      } else {
        this.setData({
          signInWay: signIn.join(',')
        })
      }
    }
    if (this.data.activityFlag && this.data.activityProcess == '') {
      wx.showToast({
        title: '请输入活动流程',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    let pages = getCurrentPages(),
      prevPage = pages[pages.length - 2];
    if (this.data.actType == 1) {
      prevPage.toggle('act.regimentSize', this.data.regimentSize);
      prevPage.toggle('act.perUpperLimit', this.data.perUpperLimit);
      prevPage.toggle('act.isCheck', this.data.isCheck);
    }
    if (this.data.actType == 2) {
      prevPage.toggle('act.invitationPeriod', this.data.invitationPeriod);
    }
    prevPage.toggle('act.applyEndTime', this.data.applyEndTime);
    prevPage.toggle('act.transportation', this.data.transportation);
    prevPage.toggle('act.signInWay', this.data.signInWay);
    prevPage.toggle('act.activityProcess', this.data.activityProcess);
    wx.navigateBack({
      delta: 1
    })
    // console.log(this.data.applyEndTime)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    //获得用户信息
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.setData({
        userInfo: userInfo
      })
      that.getTranList(options);
      that.getSignInList(options);
    })
    if (options.status) {
      this.setData({
        status: options.status
      })
    }
    if (options.invitationPeriod && options.invitationPeriod != 0) {
      this.setData({
        invitationPeriod: options.invitationPeriod,
        invitationPeriodFlag: true
      })
    }
    if (options.regimentSize && options.regimentSize != 0 && options.perUpperLimit && options.perUpperLimit != 0) {
      this.setData({
        regimentSize: options.regimentSize,
        perUpperLimit: options.perUpperLimit,
        peopelFlag: true
      })
    }
    if (options.applyEndTime && options.applyEndTime != '') {
      this.setData({
        applyEndTime: options.applyEndTime * 1,
        currentEndDate: options.applyEndTime * 1,
        currentEndTime: util.getTime(options.applyEndTime * 1),
        applyEndTimeStr: util.formatTime4(options.applyEndTime * 1)
      })
    }
    if (options.actStartTimeLong && options.actStartTimeLong != '') {
      this.setData({
        maxEndDate: options.actStartTimeLong * 1
      })
    }
    
    if (options.isCheck) {
      this.setData({
        isCheck: options.isCheck
      })
    }
    if (options.actType) {
      this.setData({
        actType: options.actType
      })
    }
    if (options.activityProcess && options.activityProcess != '') {
      this.setData({
        activityProcess: options.activityProcess,
        currentLength: options.activityProcess.length,
        // activityProcessStr: util.subRemark(options.activityProcess),
        activityProcessStr: options.activityProcess,
        activityFlag: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  }
})